javascript journal:

aqi = 99

if(aqi>0 && aqi<50) {
  console.log("Good")
} else if (aqi>51 && aqi<100) {
  console.log ("Moderate")
} else if (aqi>101 && aqi<150) {
  console.log ("Unhealthy (sensitive groups")
} else if (aqi>151 && aqi<200) {
  console.log ("Unhealthy")
} else if (aqi>201 && aqi<300) {
  console.log ("Very Unhealthy")
} else {
  console.log("Hazardous")
}

example of a loop with AND operator && or || and NOT !


let player = 0;
let computer = Math.floor(Math.random()*3) 
const choices = ['Rock', 'Paper', 'Scissors'];
here we have the player 1 and 2 as a varible and then we have a list where the names match to the numbers 0,1,2

if(player === computer) {
  console.log("Its a tie!")
} else if (
  (player === 0 && computer === 2) ||  // Rock beats Scissors
  (player === 1 && computer === 0) ||  // Paper beats Rock
  (player === 2 && computer === 1)     // Scissors beat Paper
) {
  console.log("The player won!");
} else {
  console.log("The computer won!");
}
Here is a if statement - if they are the exact same numbers - its a tie 
if not and it fits any of the 3 choices where such AND such is true then its a win for the player

if not then the computer wins.

console.log("Player picked: " + choices[player]);
console.log("Computer picked: " + choices[computer]);
prints the answer!

NESTED STATEMENTS:
if (time > 10) {
  if (coffee == true) {
    console.log("Let's start coding! 🧑‍💻");
  } else {
    console.log("Time to brew some fresh coffee. ☕️!");
  }
} else {
   console.log("Make sure to get good sleep! 😴");
}

let weather = "Sunny";
let temperature = 85;

if (weather == "Sunny") {
  if (temperature > 80) {
    console.log("Yay, let's go to the beach! 🏖️");
  } else {
    console.log("Let's do something fun outdoors! ⛰️");
  }
} else {
  console.log("Let's watch some shows on the couch! 🍿");
}

nested statements - if this is not true then goes straight to - else - then if its true and the 2nd thing is also true something else is printed if the second statement isn't true then it goes to the second else!


let randomNumber = Math.floor(Math.random()*4)
#makes sure the number isn't a decimal and gives answers 0-3 so not including 4

if (randomNumber === 0) {
  console.log("10 Push-ups");
} else if (randomNumber === 1) {
  console.log("10 Sit-ups");
} else if (randomNumber === 2) {
  console.log("10 Squats");
} else {
  console.log("10 Jumping Jacks");
}


WHILE LOOPS
As long as the condition is true, the code in the while loop will run. Otherwise, the loop ends when the condition becomes false.

let randomNumber = Math.floor(Math.random() * 10);

while (randomNumber !== 7) {
  console.log("Duck 🦆");
  randomNumber = Math.floor(Math.random() * 10);
}

console.log("Goose! 🦢");

// Write code below 💖

const luckyNumber = 7
let guess = Math.floor(Math.random()*10)+1;
floors and randomises a number between 1 and 10 +1 starts it at 1 not 0 and then also ends it at 10 not 9

while (guess != 7) {
  console.log("Nope it isn't " + guess);
  guess = Math.floor(Math.random() * 10) + 1;
}

console.log("My lucky number is 7!");

By now, you may have realized that we're updating a variable within our loops. These variables are also known as iterators.


To "iterate" means to run the code in a loop once. When the i iterator variable has reached a value that sets the loop's condition to false, we can successfully exit the loop!

for (let i = initialValue; condition; updateIterator) {
  // Code here
}


for (let i = 1; i <= 10; i++) {
  console.log(i);
}

let i = 1: initializes an iterator variable.
i <= 10: the conditional statement that is tested before each iteration.
i++: this increments the iterator variable by 1 after each iteration.
Note: The ++ operator increments a number value by 1. 
It's the same as writing i = i + 1


for (let i = 1; i <= 100; i++) {
  console.log("I Must Not Tell Lies ")
}

So when the program writes the line 100 times the loop breaks!

for (let i = 0; i < 5; i++) {
  if (i == 1) {
    continue;
  }
  console.log(i);
}

The code above will output the numbers 0 through 4, except for 1 because the continue skips this iteration.

0
2
3
4


for (let i = 0; i < 5; i++) {
  if (i == 3) {
    break;
  }
  console.log(i);
}

If and when i is equal to 3, the break keyword ends the loop, and our output looks like this:

0
1
2

for (let i = 1; i <= 50; i++) {
  if (i === 42) {
    break;
  } else if (i % 2 === 1) {
    continue;
  }
  console.log(i);
}

a loop I made - if the numbers odd it skips and if the number is equal to 42 the cycle 
breaks

// Write code below 💖
//starting number
let myNumber = 200;
//empty binary string
let binary = "";

//loop until 0
while (myNumber > 0) {
  let remainder = myNumber % 2;
  //gets the number that is remainder and converts it into a string
  // then adds that string to the string binary
  binary = remainder.toString() + binary;
  // then divdes the number by 2 for the next loop
  myNumber = Math.floor(myNumber / 2);
}
// when the loops broken the result prints as binary!
console.log(binary)


  countdown--; - this minuses -1 from the variable leading to countdown...



for the middle condition - if I is bigger than 0 then you minus 1

for (let i = 10; i > 0; i--) {
  console.log(i)
}
console.log("To infinity and beyond! 🚀")


// Write code below 💖

let count = 0
let targetNumber = Math.floor(Math.random()*100)

while (count < 10) {
  let drawNumber = Math.floor(Math.random()*100)
  if (drawNumber === targetNumber) {
    count++
    console.log("Match")
  } else {
  console.log("No Match") }
}

console.log("The number " + targetNumber + " was found " + count + " times.")

ARRAYS

Arrays are variables that can hold multiple values:
array = ["Leonardo", "Michaelangelo", "Donatello", "Raphael"];

CAN BE LET OR CONST

let myDestinations = [
  "🗽 Manhattan - Washington Square Park",
  "🏟️ The Bronx - Yankee Stadium",
  "🎡 Brooklyn - Coney Island",
  "🧧 Queens - Flushing",
  "🌉 Staten Island - Historic Richmond Town"
];

console.log(myDestinations);


console.log(nameofobj[0]);
positions start from 0

let groceryList = ["🥛 Milk", "🥑 Avocado", "🥚 Eggs", "🍞 Bread"];

groceryList[3] = "🧈 Butter";
groceryList[4] = "🧼 Laundry soap"; #adds or appends to an index that isn't there yet

console.log(groceryList)

timesing:

// Write code below 💖
let multiple = 6
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

for (let i = 0; i < numbers.length; i++) {
  console.log(multiple + " x " + i + " = " + numbers[i] * multiple);
}

myArray.unshift(newValue); #adds to beginning

myArray.push(newValue); #adds to end

 we can also remove them as well with the .shift() removes from front and .pop()  removes the last methods.

.indexOf() - tells you the index of a certain object by name

array.includes(value); - tells you whether that is in the array


console.log(`Found Waldo at index ${waldoIndex}!`); - to print variables without + adding them!

// Write code below 💖

const characters = [
  "The Wally Watchers",
  "Wilma",
  "Fritz",
  "Wizard Whitebeard",
  "Odlaw",
  "Waldo",
  "Woof"
];

waldoIndex = characters.indexOf("Waldo");
isWaldo = characters.includes("Waldo");

if (isWaldo === true) {
  console.log(`Found Waldo at index ${waldoIndex}!`);
} else {
  console.log("Not found")
}

FUNCTIONS:
BASIC PRINCIPLE:

function myFunction() {
  // Code here
}

myFunction();

// Write code below 💖

function countDown() {
  for (let i = 10; i > 0; i--) {
    console.log(i)
    }
  return "Blast Off! 🚀";
}
console.log(countDown());

// Write code below 💖

function relativityTheory(mass, light) {
  const energy = (light * light)* mass
  return console.log(`Energy = ${energy}`);
}

relativityTheory(64, 299774);


To calculate the mean and median, create two functions called mean() and median(). Each function accepts an viewsArray parameter.

// Write code below 💖

const recentTikTokViews = [1932, 2300, 453, 5222, 6733, 7402, 8334];
const recentInstagramViews = [936, 2576, 453, 7013, 5489, 7402, 3921];
const recentYouTubeViews = [2300, 453, 5222, 989, 6733, 7402, 2789];

function mean(viewsArray) {
  const totalViews = viewsArray.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
  const average = totalViews / viewsArray.length;
  return average; // give back the mean
}
console.log("TikTok", "Mean:", mean(recentTikTokViews));

function median(viewsArray) {
  const sortedViews = viewsArray.sort();
  let med = viewsArray [3]
  return med; // give back the mean
}

console.log("TikTok", "Median:", median(recentTikTokViews));


// Write code below 💖

function isPalindrome(word) {
  const lowerWord = word.toLowerCase();
  const reversedWord = lowerWord.split('').reverse().join('');
  return reversedWord === word;
  }

console.log(isPalindrome("racecar"));

Objects are used for storing multiple values in a single variable. They are usually key:value

object = {
  key1: value1,
  key2: value2,
  key3: value3
}


const pizza = {
  topping: "Pepperoni 🍕",
  type: "Hand-tossed",
  slices: 12,
  price: 14.99
}

console.log(pizza);


const car = { 
  model: "Honda",
  year: 2002, 
  color: "Blue",
  used: false
};

console.log(`I'm looking for a ${car.color} ${car.model} ${car.year} that is new.`);

pokemon = {
  name: "Pikachu",
  type: "Electric ⚡️",
  level: 25,
}

pokemon.isCaught = false
pokemon.isCaught = true
pokemon.name = "Umbreon"

console.log(pokemon)

// Write code below 💖

const pig = {
  name: "Porky",
  type: "pig",
  age: 3,
  makeSound() {
    return `${this.name} is a ${this.age} year old ${this.type} that goes oink!`;
  }
};

const sheep = {
  name: "Shaun",
  type: "sheep",
  age: 2,
  makeSound() {
    return `${this.name} is a ${this.age} year old ${this.type} that goes baa!`;
  }
};

const dog = {
  name: "Buddy",
  type: "dog",
  age: 4,
  makeSound() {
    return `${this.name} is a ${this.age} year old ${this.type} that goes woof!`;
  }
};

console.log(pig.makeSound());  // 🌟 "Porky is a 3 year old pig that goes oink!"
console.log(sheep.makeSound()); // 🐑 "Shaun is a 2 year old sheep that goes baa!"
console.log(dog.makeSound());   // 🐶 "Buddy is a 4 year old dog that goes woof!"

























